import 'dart:async';
import 'package:flutter/material.dart';
import 'course_card.dart';

class CourseList extends StatefulWidget {
  final List<CourseInfo> courses;

  const CourseList({super.key, required this.courses});

  @override
  State<CourseList> createState() => _CourseListState();
}

class _CourseListState extends State<CourseList> {
  Timer? _timer;

  @override
  void initState() {
    super.initState();
    // 每分钟刷新一次
    _timer = Timer.periodic(const Duration(minutes: 1), (timer) {
      if (mounted) setState(() {});
    });
  }

  @override
  void dispose() {
    _timer?.cancel();
    super.dispose();
  }

  @override
  Widget build(BuildContext context) {
    return Card(
      margin: const EdgeInsets.symmetric(horizontal: 12, vertical: 12),
      child: Padding(
        padding: const EdgeInsets.all(12),
        child: Column(
          children: [
            const Center(
              child: Text(
                '今日课表',
                style: TextStyle(fontSize: 20, fontWeight: FontWeight.bold),
              ),
            ),
            const SizedBox(height: 8),
            if (widget.courses.isEmpty)
              const Center(child: Text('今日无课程'))
            else
              ...widget.courses.map((course) => CourseCard(course: course)),
          ],
        ),
      ),
    );
  }
}
